/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer;


import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.OverallGUILayoutFactory;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.GameManagerMenu;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.mainmenu.MainMenuPanel;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.GameManager;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.filehandlers.IniLoader;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

/**
 * @author Massimo Canonaco
 *
 */
public class MainFrame extends JFrame {

	/** */
	private GameManagerMenu gameManagerMenu;
	
	/** */
	private MainMenuPanel mainMenuPanel;
	
	/** */
	static public final String CONFIGURATION_FILE = "cbvapps"+File.separator+"uiresource"+File.separator+"generalschema.ini";
	
	/** */
	private GameManager gameManager;
	
	/** */
	private JPanel panel;
	
	/**
	 * @throws FontFormatException  */
	public void firstLoad() throws IOException, FontFormatException
	{
		OverallGUILayoutFactory factory = OverallGUILayoutFactory.getInstance();
		IniLoader loader = new IniLoader(new HashMap<String,String>());
		loader.load(CONFIGURATION_FILE);
		Map infoMap = loader.getContents();
		factory.setUp(infoMap);
		setUpFrame(infoMap);
		mainMenuPanel = new MainMenuPanel(this);
		
//		mainMenuPanel.setUp(infoMap);
//		panel.add(mmp);
		this.getContentPane().add(panel);
	}
	
	/** */
	private void setUpFrame(Map infoMap)
	{
		final String WINDOW_WIDTH = "window.width",
						WINDOW_HEIGHT = "window.height",
						WINDOW_TITLE = "window.title",
						WINDOW_MODE = "window.mode";
		
		setSize(Integer.parseInt((String)infoMap.get(WINDOW_WIDTH)),Integer.parseInt((String)infoMap.get(WINDOW_HEIGHT)));
		setPreferredSize(new Dimension(Integer.parseInt((String)infoMap.get(WINDOW_WIDTH)),Integer.parseInt((String)infoMap.get(WINDOW_HEIGHT))));
		setMaximumSize(new Dimension(Integer.parseInt((String)infoMap.get(WINDOW_WIDTH)),Integer.parseInt((String)infoMap.get(WINDOW_HEIGHT))));
		setTitle((String)infoMap.get(WINDOW_TITLE));
		Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((screenDimension.width - this.getWidth())/2, (screenDimension.height - this.getHeight())/2);
		if(((String)infoMap.get(WINDOW_MODE)).equalsIgnoreCase("full"))
		{
			setUndecorated(true);
		}
	}
	
	public MainFrame() throws IOException
	{
		gameManager = new GameManager();
		panel = new JPanel(new BorderLayout());
//		panel.setSize(1366, 768);
		setResizable(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void switchTo(final JPanel newPanelComponent)
	{
            	panel.removeAll();
            	panel.add(newPanelComponent,BorderLayout.CENTER);
            	panel.validate();
            	panel.updateUI();
            	panel.requestFocus();
	}
	
	public void getToMain() //throws IOException, FontFormatException
	{
		switchTo(mainMenuPanel);
	}
	
	public static void create()
	{
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				MainFrame main;
				try {
					main = new MainFrame();
					main.firstLoad();
					main.getToMain();
					main.setVisible(true);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (FontFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
	}
	public static void main(String[] args) throws Throwable {
		
		
		MainFrame.create();
//		final MainFrame main = new MainFrame();
//		Thread t = new Thread(new Runnable() {
//			public void run() {
//				try {
//					main.firstLoad();
//				} catch (IOException | FontFormatException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
//				main.setVisible(true);
//				while(true)
//				{
//					main.repaint();
//					try{
//						Thread.sleep(25);
//					}catch (final InterruptedException e)
//                    {
//                        //do nothing
//                    }
//				}
//			}
//		});
//		t.setName("igpegame");
//		t.start();
	}

	/**
	 * @return the gameManager
	 */
	public GameManager getGameManager() {
		return gameManager;
	}

	/**
	 * @return the panel
	 */
	public JPanel getPanel() {
		return panel;
	}

	/**
	 * @return the gameManagerMenu
	 */
	public GameManagerMenu getGameManagerMenu() {
		return gameManagerMenu;
	}

	/**
	 * @param gameManagerMenu the gameManagerMenu to set
	 */
	public void setGameManagerMenu(GameManagerMenu gameManagerMenu) {
		this.gameManagerMenu = gameManagerMenu;
	}
	
}
